package com.db.manage.controller;

import com.db.manage.model.ConnectionConfig;
import com.db.manage.service.ConnectionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/connections")
@RequiredArgsConstructor
public class ConnectionController {
    private final ConnectionService connectionService;

    @PostMapping
    public ResponseEntity<String> addConnection(@RequestBody ConnectionConfig config) {
        try {
            // 验证必要字段
            if (config.getName() == null || config.getName().isEmpty()) {
                return ResponseEntity.badRequest().body("连接名称不能为空");
            }
            if (config.getDbType() == null || config.getDbType().isEmpty()) {
                return ResponseEntity.badRequest().body("数据库类型不能为空");
            }
            if (config.getUrl() == null || config.getUrl().isEmpty()) {
                return ResponseEntity.badRequest().body("连接地址不能为空");
            }
            if (config.getUsername() == null || config.getUsername().isEmpty()) {
                return ResponseEntity.badRequest().body("用户名不能为空");
            }
            if (config.getPassword() == null || config.getPassword().isEmpty()) {
                return ResponseEntity.badRequest().body("密码不能为空");
            }

            connectionService.addConnection(config);
            return ResponseEntity.ok("连接添加成功");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("连接添加失败: " + e.getMessage());
        }
    }

    @PostMapping("/test")
    public ResponseEntity<String> testConnection(@RequestBody ConnectionConfig config) {
        try {
            log.info("测试连接: {}", config);
            
            // 验证必要字段
            if (config.getDbType() == null || config.getDbType().isEmpty()) {
                return ResponseEntity.badRequest().body("数据库类型不能为空");
            }
            if (config.getUrl() == null || config.getUrl().isEmpty()) {
                return ResponseEntity.badRequest().body("连接地址不能为空");
            }
            if (config.getUsername() == null || config.getUsername().isEmpty()) {
                return ResponseEntity.badRequest().body("用户名不能为空");
            }
            if (config.getPassword() == null || config.getPassword().isEmpty()) {
                return ResponseEntity.badRequest().body("密码不能为空");
            }

            // 测试连接
            boolean success = connectionService.testConnection(config);
            if (success) {
                return ResponseEntity.ok("连接测试成功");
            } else {
                return ResponseEntity.badRequest().body("连接测试失败");
            }
        } catch (Exception e) {
            log.error("连接测试失败", e);
            return ResponseEntity.badRequest().body("连接测试失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<String> removeConnection(@PathVariable String id) {
        try {
            connectionService.removeConnection(id);
            return ResponseEntity.ok("连接删除成功");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("连接删除失败: " + e.getMessage());
        }
    }

    @PostMapping("/{id}/switch")
    public ResponseEntity<String> switchConnection(@PathVariable String id) {
        try {
            connectionService.switchConnection(id);
            return ResponseEntity.ok("连接切换成功");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("连接切换失败: " + e.getMessage());
        }
    }

    @GetMapping
    public ResponseEntity<List<ConnectionConfig>> getAllConnections() {
        return ResponseEntity.ok(connectionService.getAllConnections());
    }
} 